/*
 * SchedulerFacade
 * Copyright (c) 2019 taisu Inc., All rights reserved.
 * This software is the confidential and proprietary information of
 * taisu Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with tysu.com.cn.
 */

package com.ymk.base.cmd;



import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.mch.MchBO;
import com.ymk.bo.member.MemberBO;
import com.ymk.core.code.cmd.MchCmdCode;
import com.ymk.core.code.cmd.MemberCmdCode;
import com.ymk.core.constants.SysConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 公共类
 */
@Component
public class SchedulerFacade {
    private final static Logger logger = LoggerFactory.getLogger(SchedulerFacade.class);

    // 获取商户
    public MchBO getMch(HttpServletRequest request, Integer id) throws Exception {
        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("id", id);
        CmdSign mchGetCmd = CmdSign.Builder.newBuild(null)
                .setSource(paramsBuilder.build())
                .setCmdCode(MchCmdCode.MCH_GET)
                .setScope(SysConstants.CMD_SERVER_SCOPE)
                .build();
        MchBO mchBO = new MchBO();
        try {
            mchBO = this.scheduler.schedule(request, mchGetCmd, MchBO.class);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
            mchBO = null;
        }
        return mchBO;
    }

    public MemberBO getMember(Integer memberId, CmdSign sign) throws Exception {
        Map<String, Object> params = ParamsBuilder.newBuild().build();
        return this.getMember(memberId, sign, params);
    }

    public MemberBO getMember(Integer memberId, CmdSign sign, Map<String, Object> params) throws Exception {
        params.put("id", memberId);
        CmdSign memberSign = CmdSign.Builder.newBuild(sign)
                .setCmdCode(MemberCmdCode.MEMBER_GET)
                .setScope(SysConstants.CMD_SERVER_SCOPE)
                .setSource(params).build();
        return this.scheduler.schedule(null, memberSign, MemberBO.class);
    }



    @Autowired
    private SchedulerCmd scheduler;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
}
